﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer.Mapper;
using DataAccessLayer.Models.POJO;
using MySql.Data.MySqlClient;

namespace DataAccessLayer.Service
{
    public class assetLevel1Service : assetLevel1Mapper
    {

        public static MySqlConnection connection = null;


        public void create()
        {
            connection = new MySqlConnection("data source = localhost; database = dotnet; user id = root; password = 123456; pooling = true; charset = utf8; ");
        }
        public void Open()
        {
            connection.Open();
        }

        public void Close()
        {
            connection.Close();
        }

        public bool Delete(assetLevel1Peta assetLevel1)
        {
            create();
            var db = new PetaPoco.Database(connection);
            Open();
            if (db.Delete<assetLevel1Peta>("where assetLevel1Id = @0 and ownerDeptId = @1 ", assetLevel1.assetLevel1Id ,assetLevel1.ownerDeptId ) > 0)
            {
                Close();
                return true;
            }
            else
            {
                Close();
                return false;
            }
        }

        public bool Insert(assetLevel1Peta assetLevel1)
        {
            try
            {
                create();
                var db = new PetaPoco.Database(connection);
                Open();
                db.Insert(assetLevel1);
                Close();
            }
            catch (Exception ex)
            {
                Close();
                return false;
            }

            return true;
        }

        public List<assetLevel1Peta> SelectAll(string deptId)
        {
            create();
            var db = new PetaPoco.Database(connection);
            Open();
            List<assetLevel1Peta> list = db.Query<assetLevel1Peta>("select * from assetlevel1 where ownerDeptId = @0 ",deptId).ToList();
            Close();
            return list;
        }
    }
}
